package gew.system;

import gew.services.DimensionValueService;
import gew.services.LocaleService;
import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;

public class RequestParameterBase implements IRequestParameter {

    static final String BUNDLE_NAME = "labels";
    private Locale locale;
    private String localeUrlCode;
    private final String serverName;
    private final int serverPort;
    private final String contextPath;
    private String section;
    private final ArrayList<String> params;

    ArrayList<String> getParams() {
        return params;
    }

    private void initSection(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.SECTION)) {
            section = request.getParameterMap().get(Parameters.SECTION)[0];
        } else if (params.contains(Parameters.SECTION)) {
            section = request.getAttribute(Parameters.SECTION).toString();
        } else {
            section = "not specified";
        }
    }

    private void initLocale(final HttpServletRequest request) {
        if (request.getParameterMap().containsKey(Parameters.LOCALE)) {
            localeUrlCode = LocaleService.getLocaleCode(request.getParameterMap().get(Parameters.LOCALE)[0]);
        } else if (params.contains(Parameters.LOCALE)) {
            localeUrlCode = LocaleService.getLocaleCode(request.getAttribute(Parameters.LOCALE).toString());
        } else {
            localeUrlCode = LocaleService.EN;
        }
        locale = LocaleService.getLocale(localeUrlCode);
    }

    void init(final HttpServletRequest request, final DimensionValueService dvs) {

    }

    public RequestParameterBase(HttpServletRequest request, DimensionValueService dvs) {
        serverName = request.getServerName();
        serverPort = request.getServerPort();
        contextPath = request.getContextPath();

        Enumeration<String> pEnum = request.getAttributeNames();
        params = new ArrayList<>();
        while (pEnum.hasMoreElements()) {
            params.add(pEnum.nextElement());
        }

        initSection(request);
        initLocale(request);
        init(request, dvs);

    }

    @Override
    public Locale getLocale() {
        return locale;
    }

    @Override
    public String getSection() {
        return section;
    }

    static String getIdsHS(List<Integer> ids) {
        String r = SqlUtils.EMPTY;
        for (int i = 0; i < ids.size(); i++) {
            r = i == 0
                    ? ids.get(i).toString()
                    : r.concat(SqlUtils.COMMA)
                    .concat(ids.get(i).toString());
        }
        return r;
    }

    @Override
    public String getServerName() {
        return serverName;
    }

    @Override
    public Integer getServerPort() {
        return serverPort;
    }

    @Override
    public String getHttpHeader() {
        return serverPort == 80
                ? SqlUtils.HTTP.concat(this.serverName)
                : SqlUtils.HTTP.concat(this.serverName)
                .concat(SqlUtils.COLON)
                .concat(getServerPort().toString());
    }

    @Override
    public String getContextPath() {
        return contextPath;
    }

    @Override
    public String getLocaleUrlCode() {
        return localeUrlCode;
    }

    static final String getInitCountryUrlCode(final HttpServletRequest request, final ArrayList<String> params, final Locale locale) {
        String result;
        if (request.getParameterMap().containsKey(Parameters.COUNTRY)) {
            result = request.getParameterMap().get(Parameters.COUNTRY)[0];
        } else if (params.contains(Parameters.COUNTRY)) {
            result = request.getAttribute(Parameters.COUNTRY).toString();
        } else {
            result = ResourceBundle.getBundle(BUNDLE_NAME, locale).getString(LabelCode.PARAM_DEFAULT_COUNTRY_CODE_VALUE);
        }
        return result;
    }

    static final String getInitIndicatorUrlCode(final HttpServletRequest request, final ArrayList<String> params, final Locale locale) {
        String result;
        if (request.getParameterMap().containsKey(Parameters.INDICATOR)) {
            result = request.getParameterMap().get(Parameters.INDICATOR)[0];
        } else if (params.contains(Parameters.INDICATOR)) {
            result = request.getAttribute(Parameters.INDICATOR).toString();
        } else {
            result = ResourceBundle.getBundle(BUNDLE_NAME, locale).getString(LabelCode.PARAM_DEFAULT_INDICATOR_CODE_VALUE);
        }
        return result;
    }
}
